<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/jsp/common/include/taglibs.jsp"%>
<script type="text/javascript">

$(document).ready(function(){

});

function updateCnt(){
	getEnc();
	$.ajax({
		url: "./updateCnt.do",
		dataType: "json",
		data:$("#frm").serialize(),
		success : function (json){
			if("0"==json.returnCd){
				showMsg("저장되었습니다");
			}else{
				showMsg("저장실패");
			}
		},
		error : function(error){
			seqChk = true;
			alert('접속지연이 발생했습니다. 몇분후에 다시 확인해주세요\n'+error);
		}
	});
}

//데이터 저장
function submitData(){
	getEnc();
	$("input[name='hps']").each(function(){
		if(""==$(this).val()){
			$(this).parent().parent().remove();
		}else{
			$(this).val($(this).val().split("-").join(""));
		}
	});
// 	$("#hp").val($("#hp").val().split("-").join(""));
	$.ajax({
		url: "./updateUsr.do",
		dataType: "json",
		data:$("#frm").serialize(),
		success : function (json){
			if("0"==json.returnCd){
				showMsg("저장되었습니다");
			}else if("1"==json.returnCd){
				var alertMsg = "";
				$.each(json.list, function (i) {
					if(""!=alertMsg){
						alertMsg+="\n";
					}
					alertMsg+="회원번호 : "+this.mem_seq+", 닉네임 : " + this.nick+", 전화번호 : "+this.hp;
				});
				alertMsg+="과 중복됩니다";
				showMsg(alertMsg);
			}else{
				showMsg("저장실패");
			}
		},
		error : function(error){
			seqChk = true;
			alert('접속지연이 발생했습니다. 몇분후에 다시 확인해주세요\n'+error);
		}
	});
	
}

function goList(){
	location.href="./main.do";
}
function getEnc(){
	if(window.hwInfo){
		$("#enc").val(window.hwInfo.getInfo());	
	}
}

function showMsg(msg){
	if(window.hwInfo){
		$("#enc").val(window.hwInfo.showMsg(msg));	
	}else{
		alert(msg);
	}
}

var addUsr =1;
function addUsrs(){
	addUsr++;
	var html= "<tr id='hp_"+addUsr+"'>"+
	"	<td>연락처</td>"+
	"	<td><input name=\"hps\" type=\"tel\" value=\"\" /><button onclick=\"removeUsr('hp_"+addUsr+"');\">삭제</button> </td>"+
	"</tr>";
	$("#hp").after(html);
}

function removeUsr(id){
	$("#"+id).remove();
}
</script>

<form id="frm" action="./updateUsr.do" method="post" >
<input id="enc" name="enc" value="" type="hidden"/>
<input id="mem_seq" name="mem_seq" value="${map.mem_seq }" type="hidden"/>
<table>
	<tr>
		<td>닉네임</td>
		<td><input name="nick" type="text" value="${map.nick }" /> </td>
	</tr>
<c:set var="hp_arr" value="${fn:split(map.hp,',')}"/>
<c:forEach var="a_hp" items="${hp_arr}" varStatus="stat">
	<tr <c:if test="${stat.index ==0 }">id="hp"</c:if>>
		<td>연락처</td>
		<td>
			<c:if test="${fn:length(a_hp)==11 }"><c:set var="frm_hp" value="${fn:substring(a_hp,0,3) }-${fn:substring(a_hp,3,7) }-${fn:substring(a_hp,7,11) }"/></c:if>
			<c:if test="${fn:length(a_hp)==10 }"><c:set var="frm_hp" value="${fn:substring(a_hp,0,3) }-${fn:substring(a_hp,3,6) }-${fn:substring(a_hp,6,10) }"/></c:if>
			<c:if test="${fn:length(a_hp)!=10 && fn:length(a_hp)!=11 }"><c:set var="frm_hp" value="${a_hp }"/></c:if>
		<input name="hps" type="tel" value="${frm_hp }" />
		
		<c:if test="${stat.index ==0 }"><button onclick="addUsrs();return false;">추가</button></c:if>
		<c:if test="${stat.index !=0 }"><button onclick="$(this).parent().parent().remove();return false;">삭제</button></c:if>
		</td>
	</tr>
</c:forEach>
<!-- 	<tr> -->
<!-- 		<td>연락처</td> -->
<%-- 		<td><input name="hp" id="hp" type="tel" value="<c:if test="${fn:length(map.hp)==11 }">${fn:substring(map.hp,0,3) }-${fn:substring(map.hp,3,7) }-${fn:substring(map.hp,7,11) }</c:if><c:if test="${fn:length(map.hp)==10 }">${fn:substring(map.hp,0,3) }-${fn:substring(map.hp,3,6) }-${fn:substring(map.hp,6,10) }</c:if><c:if test="${fn:length(map.hp)!=10 && fn:length(map.hp)!=11 }">${map.hp }</c:if>" /> --%>
<!-- 		</td> -->
<!-- 	</tr> -->
	<tr>
		<td>등급</td>
		<td>
			<select name="user_type">
				<option value="">선택하세요</option>
	<c:if test="${cd2List !=null && fn:length(cd2List)>0 }">
		<c:forEach var="cd" items="${cd2List}" varStatus="status">
				<option value="${cd.code_no }" <c:if test="${map.user_type==cd.code_no }">selected="selected"</c:if>>${cd.code_nm }</option>
		</c:forEach>
	</c:if>
			</select>
<%-- 			<input name="user_type" type="text" value="${map.user_type }" /> --%>
		</td>
	</tr>
	<tr>
		<td>사이트</td>
		<td>
			<select name="con_site">
				<option value="">선택하세요</option>
	<c:if test="${cd1List !=null && fn:length(cd1List)>0 }">
		<c:forEach var="cd" items="${cd1List}" varStatus="status">
				<option value="${cd.code_no }" <c:if test="${map.con_site==cd.code_no }">selected="selected"</c:if>>${cd.code_nm }</option>
		</c:forEach>
	</c:if>
			</select>
<%-- 		<input name="con_site" type="text" value="${map.con_site }" /> --%>
		</td>
	</tr>
	<tr>
		<td>비밀특이사항</td>
		<td><input name="sec_etc" type="text" value="${map.sec_etc }" /> </td>
	</tr>
	<tr>
		<td>특이사항</td>
		<td><input name="etc" type="text" value="${map.etc }" /> </td>
	</tr>
</table>
</form>
<button onclick="updateCnt();">방문카운트 올리기</button>
<button onclick="submitData();">저장</button>
<button onclick="goList();">목록</button>

